package com.mininglamp.action;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.mininglamp.model.Orders;
import com.mininglamp.service.OrderService;
import com.mininglamp.utils.ExcelOperations;
import com.mininglamp.utils.Pager;
import com.mininglamp.utils.ResultMsg;

@RestController
@RequestMapping("/order")
public class OrderAction {

	
	 @Value("${xls.ordersTemplate}")
	 private String ordersTemplate;
	
	@Autowired
	OrderService orderService;
	
	/**
	 * 新增订单
	 * @param order
	 * @return
	 */
	@PostMapping(value="/insert")
	public ResultMsg insertOrder(@RequestBody Orders order){
		
		int flag = orderService.insertOrder(order);
		String msg ="";
		if(flag==1){
			msg = "success";
		}else if(flag==0){
			msg = "false";
		}
		return ResultMsg.success(msg);
	}
	/**
	 * 查询分页订单信息
	 * @param pager
	 * @param wetChatsId
	 * @return
	 */
	@GetMapping(value="/search/{wetChatsId}")
	public ResultMsg searchPageOrders(@Valid Pager pager,@PathVariable String wetChatsId){
		
		return ResultMsg.success(orderService.findPageOrders(wetChatsId, pager));
		
	}
	
	@PostMapping(value="/import")
	public ResultMsg importOrder(@RequestParam("file") MultipartFile file){
		
		try {
			List<String[]> list = ExcelOperations.readExcelFile(file.getInputStream());
			Orders orders = null;
			for(String[] ss:list){
				orders = new Orders();
				orders.setWetchatId(ss[0]);
				orders.setPhone(ss[1]);
				orders.setReceiptName(ss[2]);
				orders.setSpecification(ss[3]);
				orders.setAddress(ss[4]);
				orders.setCourierId(ss[5]);
				orderService.insertOrder(orders);
			}
			return ResultMsg.success("文件上传成功");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ResultMsg.success("文件上传失败");
		}
		
	}
	@GetMapping(value="/export/{wetChatId}")
	public void exportOrder(@PathVariable String wetChatId,HttpServletResponse response) throws Exception{
		 response.setHeader("Content-Disposition", "attachment; filename="+ UUID.randomUUID().toString().replace("-","").toUpperCase()+".xls");
	     response.setContentType("application/vnd.ms-excel; charset=utf-8") ;
	     OutputStream out = response.getOutputStream();
	     
	     ExcelOperations.writeOrdersExcel(ordersTemplate, orderService.findByWetchasId(wetChatId)).write(out);
	     out.flush();
	     out.close();
	}
}
